package com.pingan.haofang.searchcloud.api.query.parser.dbobject;

import com.pingan.haofang.searchcloud.api.query.parser.dbobject.sqlnode.function.Function;
import com.pingan.haofang.searchcloud.api.query.parser.exception.SqlParseException;

public class FunctionFactory {

    public static Function getFunction(String name, boolean distinct) {
        FunctionConfig config = FunctionConfig.getFunction(name);
        if (config == null) {
            return null;
        }

        // 针对count(distinct)判断
        if (config == FunctionConfig.COUNT && distinct) {
            config = FunctionConfig.COUNT_DISTINCT;
        }

        try {
            Function function = config.functionClass.newInstance();
            return function;
        } catch (Exception e) {
            throw new SqlParseException(e.getMessage(), e);
        }
    }
}
